package com.example.domoa;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnSend;
    private Button btnSendLocal;
    private MyBroadcastReceiver receiver;
    private HightReceiver hReceiver;
    private MidReceiver mReceiver;
    private LowReceiver lReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerLocalReceiver();

        receiver=new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("weather_report");
        registerReceiver(receiver,filter);

        IntentFilter hfilter = new IntentFilter();
        hfilter.addAction("weather_report");
        hfilter.setPriority(100);
        hReceiver = new HightReceiver();
        registerReceiver(hReceiver,hfilter);

        IntentFilter mfilter = new IntentFilter();
        mfilter.addAction("weather_report");
        mfilter.setPriority(50);
        mReceiver = new MidReceiver();
        registerReceiver(mReceiver,mfilter);

        IntentFilter lfilter = new IntentFilter();
        lfilter.addAction("weather_report");
        lfilter.setPriority(0);
        lReceiver = new LowReceiver();
        registerReceiver(lReceiver,lfilter);

        btnSend = findViewById(R.id.btn_send);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("weather_report");
                sendOrderedBroadcast(intent,null);
            }
        });

        btnSendLocal = findViewById(R.id.btn_send_local);
        btnSendLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocalBroadcastManager manager =
                        LocalBroadcastManager.getInstance(MainActivity.this);
                Intent intent = new Intent();
                intent.setAction("traffic_report");
                manager.sendBroadcast(intent);
            }
        });
    }

    private void registerLocalReceiver() {
        receiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("traffic_report");
        LocalBroadcastManager manager =
                LocalBroadcastManager.getInstance(MainActivity.this);
        manager.registerReceiver(receiver,filter);
    }
}
